<template>
    <div>
      <div style="padding: 10px 0;">
      <el-input style="width: 200px;" placeholder="请输入名称搜索" v-model="menuName" suffix-icon="el-icon-search" >
      </el-input>
      <el-button type="primary" style="margin-left: 10px;" @click="load()"> 搜索</el-button>
      <el-button type="primary" style="margin-left: 10px;" @click="reset()"> 重置</el-button>
      </div>
      <div style="padding: 10px 0;">
      <el-button type="primary" style="margin-left: 10px;" @click="add()"> 新增</el-button>
      <el-button type="primary" style="margin-left: 10px;" @click="delAll()"> 批量删除</el-button>
      </div>

<el-dialog title="新增菜单" :visible.sync="dialogFormVisible" width="30%">
    <el-form label-width="100px" :model="form" size="small">
         <el-form-item label="菜单名称" >
            <el-input v-model="form.menuName" autocomplete="off"></el-input>
         </el-form-item>

         <el-form-item label="路由" >
            <el-input v-model="form.path" autocomplete="off"></el-input>
         </el-form-item>

         <el-form-item label="组件路径" >
            <el-input v-model="form.component" autocomplete="off"></el-input>
         </el-form-item>
    </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="addMenu()" >确 定</el-button>
  </div>
</el-dialog>

<el-dialog title="修改菜单" :visible.sync="editFormVisible" width="30%">
    <el-form label-width="100px" :model="form" size="small">
         <el-form-item label="菜单名称" >
            <el-input v-model="form.menuName" autocomplete="off"></el-input>
         </el-form-item>

         <el-form-item label="路由" >
            <el-input v-model="form.path" autocomplete="off"></el-input>
         </el-form-item>

         <el-form-item label="组件路径" >
            <el-input v-model="form.component" autocomplete="off"></el-input>
         </el-form-item>
    </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="editFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="editMenu()" >确 定</el-button>
  </div>
</el-dialog>

<el-table
    :data="tableData"
    style="width: 100%;margin-bottom: 20px;"
    row-key="menuId"
    :border="true"
    default-expand-all
    :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
    <el-table-column
      prop="menuName"
      label="日期"
      sortable
      width="180">
    </el-table-column>
    <el-table-column
      prop="path"
      label="姓名"
      sortable
      width="180">
    </el-table-column>
    <el-table-column
      prop="component"
      label="地址">
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button type="text" size="small" v-if="!scope.row.parentId && !scope.row.path" @click="add(scope.row.menuId)">新增子菜单</el-button>
            <el-button type="text" size="small" @click="edit(scope.row)">修改</el-button>
            <el-popconfirm
                style="margin-left: 5px;"
                confirm-button-text='好的'
                cancel-button-text='不用了'
                icon="el-icon-info"
                icon-color="red"
                @confirm="del(scope.row)"
                title="您确定删除吗？">
            <el-button type="text" slot="reference" size="small">删除</el-button>
            </el-popconfirm>

          </template>
        </el-table-column>
  </el-table>

      <!-- <el-table :data="tableData" row-key="tableData.menuId"
      default-expand-all @selection-change="handleSelectionChange"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="menuId" label="编号" v-if="false">
        </el-table-column>
        <el-table-column prop="menuName" label="菜单名称">
        </el-table-column>
        <el-table-column prop="path" label="路由" >
        </el-table-column>
        <el-table-column prop="component" label="组件路径" >
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="edit(scope.row)">修改</el-button>
            <el-popconfirm
                style="margin-left: 5px;"
                confirm-button-text='好的'
                cancel-button-text='不用了'
                icon="el-icon-info"
                icon-color="red"
                @confirm="del(scope.row)"
                title="您确定删除吗？">
            <el-button type="text" slot="reference" size="small">删除</el-button>
            </el-popconfirm>

          </template>
        </el-table-column>
      </el-table> -->

  <div class="block" style="padding: 20px 0;">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[2, 5, 10, 15]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total=total>
    </el-pagination>
  </div>

    </div>
</template>
<script>
import { findAll, save, update, delMenu } from '../api/Menu.js'

export default {
  data () {
    return {
      tableData: [],
      multipleSelection: [],
      flag: false,
      asidWith: '',
      iconFold: 'el-icon-s-fold',
      total: 0,
      pageNum: 1,
      pageSize: 2,
      menuName: '',
      dialogFormVisible: false,
      editFormVisible: false,
      form: {}
    }
  },
  created () {
    this.load();
  },
  computed: {
  },
  methods: {
    load () {
      findAll().then(res => {
        console.log(res)
        this.tableData = res
      }).catch(err => {
        console.log(err)
      })
    },

    reset () {
      this.orderName = '';
      this.load();
    },
    resetForm () {
      this.form = {}
    },
    handleSizeChange (pageSize) {
      console.log(pageSize);
      this.pageSize = pageSize
      this.load();
    },
    handleCurrentChange (pageNum) {
      console.log(pageNum);
      this.pageNum = pageNum
      this.load();
    },
    add (menuId) {
      this.dialogFormVisible = true
      this.form = {}
      if (menuId) {
        this.form.parentId = menuId
      }
    },
    del (row) {
      delMenu(row).then(res => {
        this.$message.success('删除成功')
        this.load()
      })
    },
    edit (row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.editFormVisible = true
    },
    editMenu () {
      update(this.form).then(res => {
        this.$message.success('修改成功')
      })
      this.editFormVisible = false
      this.resetForm()
      this.load()
    },
    search () {
      console.log('搜索')
    },
    addMenu () {
      save(this.form).then(res => {
        this.$message.success('添加成功')
        this.load()
      })
      this.dialogFormVisible = false
      this.resetForm()
    },
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },
    delAll () {
      this.multipleSelection.forEach(item => {
        delMenu(item).then(res => {
          this.$message.success('删除成功')
          this.load()
        })
      })
    }

  }
}
</script>
<style lang="scss" scoped>

</style>
